package com.example.transaction.sys.controller;

import com.example.transaction.common.vo.Result;
import com.example.transaction.sys.entity.Address;
import com.example.transaction.sys.entity.Announcement;
import com.example.transaction.sys.entity.User;
import com.example.transaction.sys.service.IAddressService;
import com.example.transaction.sys.service.IAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh
 * @since 2023-06-20
 */
@RestController
@RequestMapping("/sys/announcement")
public class AnnouncementController {

    @Autowired
    private IAnnouncementService announcementService;

    @PostMapping
    public Result<String> addAnnouncement(@RequestBody Announcement announcement){
        try {
            announcementService.save(announcement);
        }catch (Exception e){
            return Result.fail("公告新建失败");
        }
        return Result.success("公告添加成功");
    }
    @DeleteMapping("/{announcementid}")
    public Result<User>deleteAnnouncementById(@PathVariable("announcementid")Integer announcementid){
        announcementService.removeById(announcementid);
        return Result.success("删除公告成功");
    }

    @GetMapping("/{announcementid}")
    public Result<Announcement>getAnnouncementById(@PathVariable("announcementid")Integer announcementid){
        Announcement announcement=announcementService.getById(announcementid);
        return Result.success(announcement);
    }

    /**
     * 根据公告id更新公告
     * @param announcement
     * @return
     */
    @PutMapping("/updateByID")
    public Result<?>updateAnnouncement(@RequestBody Announcement announcement){
        try {
            announcementService.updateById(announcement);
        }catch (Exception e){
            return Result.fail("修改失败");
        }
        return Result.success("修改公告成功");
    }
}
